package com.zzc.fwzpxt.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zzc.fwzpxt.entity.*;
import com.zzc.fwzpxt.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: XiaoBingBy
 * Email: XiaoBingBy@qq.com
 * Date: 2017/9/28
 * Time: 8:49
 * Describe:
 */
@Controller
public class BlogIndexController {

    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private IBannerService iBannerService;
    @Autowired
    private ITeamService iTeamService;
    @Autowired
    private IProductService iProductService;
    @Autowired
    private ICommentService iCommentService;
    @RequestMapping(value = {"/", "index"})
    public String index(Model model) {
        // 最新文章
        EntityWrapper<Article> articleEntityWrapper = new EntityWrapper<>();
        articleEntityWrapper.eq("status",1);
        articleEntityWrapper.eq("article_type", 5);
        articleEntityWrapper.orderBy("gmt_create", false);
        Page<Article> articlePage = new Page<>(1, 3);
        Page<Article> news = iArticleService.selectPage(articlePage, articleEntityWrapper);
        model.addAttribute("news", news.getRecords());

        // 最新资讯
        EntityWrapper<Article> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("status",1);
        entityWrapper.eq("article_type",6);
        entityWrapper.orderBy("gmt_create", false);
        Page<Article> industry = iArticleService.selectPage(articlePage,entityWrapper);
        model.addAttribute("industrys", industry.getRecords());
        // 人才培训
        EntityWrapper<Article> talentTrainingWrapper = new EntityWrapper<>();
        talentTrainingWrapper.eq("article_type", 1);
        Article talentTraining = iArticleService.selectOne(talentTrainingWrapper);
        model.addAttribute("talentTraining", talentTraining);

        //关于我们
        EntityWrapper<Article> aboutWrapper = new EntityWrapper<>();
        aboutWrapper.eq("article_type", 2);
        Article about = iArticleService.selectOne(aboutWrapper);
        model.addAttribute("about", about);

        //最新产品
        EntityWrapper<ProductVo> productEntityWrapper = new EntityWrapper<>();
        productEntityWrapper.eq("product.status",1);
        productEntityWrapper.orderBy("gmt_create", false);
        Page<ProductVo> productPage = new Page<>(1, 6);
        Page<ProductVo> products = iProductService.selectMyPage(productPage, productEntityWrapper);
        productPage.setSize(12);
        Page<ProductVo> products12 = iProductService.selectMyPage(productPage, productEntityWrapper);
        List<List<ProductVo>> productVoLists = new ArrayList<>();
        for(int i=0; i< products.getRecords().size()/2;i++){
            List<ProductVo> productVoList = new ArrayList<>();
            for(int j=0;j<2;j++){
                productVoList.add(products.getRecords().get(j+i*2));
            }
            productVoLists.add(productVoList);
        }
        model.addAttribute("products", productVoLists);
        model.addAttribute("products12", products12.getRecords());



        //banner部分
        EntityWrapper<Banner> bannerEntityWrapper = new EntityWrapper<>();
        List<Banner> banners  = iBannerService.selectList(bannerEntityWrapper);
        model.addAttribute("banners",banners);
        //精英团队
        EntityWrapper<Team> teamEntityWrapper = new EntityWrapper<>();
        teamEntityWrapper.orderBy("sort", true);
        List<Team> teams = iTeamService.selectList(teamEntityWrapper);
        List<List<Team>> lists = new ArrayList<>();
        for(int i=0; i< teams.size()/4;i++){
            List<Team> teamList = new ArrayList<>();
            for(int j=0;j<4;j++){
                teamList.add(teams.get(j+i*4));
            }
            lists.add(teamList);
        }
        model.addAttribute("teams",lists);
        //留言
        EntityWrapper<Comment> commentEntityWrapper =new EntityWrapper();
        commentEntityWrapper.eq("status",2);
        commentEntityWrapper.orderBy("gmt_create", false);
        List<Comment> comments = iCommentService.selectList(commentEntityWrapper);
        model.addAttribute("comments",comments);
        return "/blog/index";

    }

    /**
     * 人才培训
     * @return
     */
    @GetMapping(value = "talent_training")
    public String talentTraining(Model model) {
        EntityWrapper<Article> wrapper = new EntityWrapper<>();
        wrapper.eq("article_type", 1);
        Article article = iArticleService.selectOne(wrapper);
        model.addAttribute("article", article);
        return "/blog/talent_training";
    }

    /**
     * 联系我们
     * @return
     */
    @GetMapping(value = "/contactus")
    public String contactus() {
        return "/blog/contactus";
    }

    /**
     * 关于我们
     * @return
     */
    @GetMapping(value = "/about")
    public String about(Model model) {
        EntityWrapper<Article> wrapper = new EntityWrapper<>();
        wrapper.eq("article_type", 2);
        Article article = iArticleService.selectOne(wrapper);
        model.addAttribute("article", article);
        return "/blog/about";
    }

}
